

import os
import sys
import zlib
import time
import cPickle


import rpyc
from rpyc.utils import server

import dexen
from dexen.system.server import db
from dexen.system.common import service
from dexen.system.server.core import ServerCore
from dexen.system.common.service import DexenAddr

#===============================================================================
# Globals
#===============================================================================
core = None
start_time = None
svr_name = None

#===============================================================================
# The Service Class
#===============================================================================
class ServerService(dexen.Service):
    ALIASES = ["DEXEN_SERVER"]

    def on_connect(self):
        pass
    
    def on_disconnect(self):
        pass

    def get_name(self):
        return svr_name

    def get_start_time(self):
        return start_time
    
    def start_job(self, schema_zip, job_name):
        schema_zip = rpyc.classic.obtain(schema_zip)
        core.start_master(schema_zip, job_name)
    
    def stop_job(self, job_id):
        core.stop_master(job_id)
    
    def delete_job(self, job_id):
        core.del_master(job_id)
    
    def get_node_states(self):
        return core.get_node_states()
    
    def get_job_states(self):
        return core.get_job_states()
    
    def get_job_info(self, job_id):
        return core.get_job_state(job_id)
    
    def get_actions(self, job_id, act_id, num_actions=0):
        return core.get_actions(job_id, act_id, num_actions)
    
    def num_actions(self, job_id):
        return core.num_actions(job_id)

    def get_ip(self):
        return service.get_my_ip()
    
    def get_ind_archive(self, job_id, ind_id, name):
        return core.get_ind_archive(job_id, ind_id, name)
    
    def get_ind_archive_all(self, job_id, ind_id):
        return core.get_ind_archive_all(job_id, ind_id)
    
    def get_ind_archive_names(self, job_id, ind_id):
        return core.get_ind_archive_names(job_id, ind_id)

    def get_archives_in_batch(self, job_id, archives_info_list, batch_size):
        archives_info_list = zlib.decompress(archives_info_list)
        archives_info_list = cPickle.loads(archives_info_list)
        return core.get_archives_in_batch(job_id, archives_info_list, batch_size)

    def get_job_def(self, job_id):
        return core.get_job_def(job_id)


#===============================================================================
# 
#===============================================================================

def start(reg_name, is_quiet):
    global core, reg_addr, start_time, svr_name

    # start time of the server
    start_time = time.time()
    svr_name = reg_name
    
    # creates the database for the server
    db.initialize()

    if service.is_ip_addr(reg_name):
        my_ip = reg_name
    else:
        my_ip = service.get_my_ip(service.is_local(reg_name))

    #if sys.platform == "linux2": #TODO: TEMPORARY FIX FOR AMAZON
    #    my_ip = "107.20.211.237"
    
    print "My ip:", my_ip
    
    _logger = service.get_rpyc_logger("DEXEN_SERVER", is_quiet)
    srvc = server.ThreadedServer(service=ServerService, 
                                protocol_config=service.default_config,
                                auto_register=False,
                                logger=_logger)
    my_addr = DexenAddr(my_ip, srvc.port)
    reg_addr = service.get_reg_addr(reg_name)
    conn = reg_addr.get_conn()
    conn.root.register_server(start_time, my_addr)
    core = ServerCore(reg_addr)
    core.start()
    print "Dexen server has started successfully."
    srvc.start()     
    
#===============================================================================
# 
#===============================================================================
def main():
    pass

if __name__ == '__main__':
    main()

